package com.tuanzi.loan.web.bean.family;

import com.google.gson.JsonArray;
import com.starbaba.model.utils.JSONUtils;
import com.tuanzi.loan.business.entity.family.LoanNewCreditCardInfo;
import com.tuanzi.loan.business.entity.family.LoanNewCreditCardProgress;
import com.tuanzi.loan.business.service.family.LoanNewCreditCardInfoService;
import com.tuanzi.loan.business.service.family.LoanNewCreditCardProgressService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.util.GsonUtils;
import com.tuanzi.loan.vo.view.family.CreditCardRightInfo;
import com.tuanzi.loan.vo.view.family.LoanNewCreditCardInfoVo;
import com.tuanzi.loan.web.bean.CurrentUserBean;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import com.tuanzi.server.loan_base.constant.LoanRedisKey;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.primefaces.event.FileUploadEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;
import java.util.Objects;

/**
 * User: liangZongXi
 * Create time: 14:16 2018/9/4
 * Description:
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanNewCreditCardInfoBean extends JSFInfoBean<LoanNewCreditCardInfo> {
    private static final long serialVersionUID = -929885855528526401L;

    @Autowired
    private LoanNewCreditCardInfoService loanNewCreditCardInfoService;
    @Autowired
    private CurrentUserBean currentUserBean;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private LoanNewCreditCardProgressService loanNewCreditCardProgressService;

    private LoanNewCreditCardInfoVo loanNewCreditCardInfoVo;
    private List<LoanNewCreditCardProgress> loanNewCreditCardProgressList;

    @PostConstruct
    private void init() {
        String idStr = getParamFromRequest("id");

        if(Objects.isNull(loanNewCreditCardInfoVo)){
            loanNewCreditCardInfoVo = new LoanNewCreditCardInfoVo();
            loanNewCreditCardInfoVo.getCardList().add(new CreditCardRightInfo());
        }

        if(StringUtils.isBlank(idStr)){
            $ = new LoanNewCreditCardInfo();
            changeStatusToEdit();
            return;
        }

        long id = NumberUtils.toLong(idStr);

        $ = loanNewCreditCardInfoService.findOne(id);
        if(Objects.isNull($)){
            forceRedirect(currentUserBean.getBack());
            return;
        }

        initProperties($);
        loanNewCreditCardProgressList = loanNewCreditCardProgressService.findAllByStatus(1);
    }


    private void initProperties(LoanNewCreditCardInfo loanNewCreditCardInfo){
        //卡权益
        String right = loanNewCreditCardInfo.getRightList();
        if(StringUtils.isNotBlank(right)){
            loanNewCreditCardInfoVo.getCardList().clear();
            JsonArray rightArray = JSONUtils.toJsonArray(right);
            rightArray.forEach(a -> loanNewCreditCardInfoVo.getCardList().add(GsonUtils.toObject(a.toString(), CreditCardRightInfo.class)));
        }

        //卡描述
        String cardDesc = loanNewCreditCardInfo.getCardDesc();
        if(StringUtils.isNotBlank(cardDesc)){
            JsonArray cardDescArray = JSONUtils.toJsonArray(cardDesc);
            if(cardDescArray.size() >=2){
                loanNewCreditCardInfoVo.setDetailOne(cardDescArray.get(0).getAsString());
                loanNewCreditCardInfoVo.setDetailTwo(cardDescArray.get(1).getAsString());
            }else if(cardDescArray.size() == 1){
                loanNewCreditCardInfoVo.setDetailOne(cardDescArray.get(0).getAsString());
            }

        }

        //信用卡便签
        String cardLabel = loanNewCreditCardInfo.getCardLabel();
        if(StringUtils.isNotBlank(cardLabel)){
            JsonArray cardLabelArray = JSONUtils.toJsonArray(cardLabel);
            cardLabelArray.forEach(a -> loanNewCreditCardInfoVo.getCardLabelList().add(a.getAsString()));
        }
    }


    public void add(){
        CreditCardRightInfo creditCardRightInfo = new CreditCardRightInfo();
        loanNewCreditCardInfoVo.getCardList().add(creditCardRightInfo);
        addSuccessfullyMessage();
    }

    public void uploadLogo(FileUploadEvent event) {
        String url = this.uploadImg(event.getFile());
        $.setLogo(url);
        addSuccessfullyMessage();
    }

    public void saveInfo(){
        //卡权益转换
        List<CreditCardRightInfo> cardList = loanNewCreditCardInfoVo.getCardList();
        JsonArray jsonArray = JSONUtils.toJsonArray(cardList);
        $.setRightList(jsonArray.toString());

        //卡描述
        JsonArray cardDesc = new JsonArray();
        if(StringUtils.isNotBlank(loanNewCreditCardInfoVo.getDetailOne())){
            cardDesc.add(loanNewCreditCardInfoVo.getDetailOne());
        }
        if(StringUtils.isNotBlank(loanNewCreditCardInfoVo.getDetailTwo())){
            cardDesc.add(loanNewCreditCardInfoVo.getDetailTwo());
        }
        $.setCardDesc(cardDesc.toString());

        if($.getId() != null){
            stringRedisTemplate.delete(String.format(LoanRedisKey.LOAN_NEW_CREDIT_CARD_DETAIL, $.getId()));
            stringRedisTemplate.delete(LoanRedisKey.LOAN_NEW_CREDIT_CARD_LIST);
        }

        //信用卡标签
        List<String> cardLabelList = loanNewCreditCardInfoVo.getCardLabelList();
        $.setCardLabel(JSONUtils.toJsonArray(cardLabelList).toString());
        this.$ = loanNewCreditCardInfoService.saveOrUpdate($);
       // this.$ = loanNewCreditCardInfoService.findOne($.getId());

        changeStatusToView();
        addSuccessfullyMessage();
    }
}
